package com.senjoeson.publicsource.java;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

import com.senjoeson.publicsource.MyPagerAdapter;
import com.senjoeson.publicsource.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @author sunqiao
 * @date 2018/5/28 10:49
 */

public abstract class BaseActivity extends AppCompatActivity {
    @Nullable
    @BindView(R.id.toolbar)
    Toolbar mToolbar;

    public Unbinder mUnbinder;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(setResId());
        mUnbinder = ButterKnife.bind(this);
        initToolbar(setMainTitle());

        initView();
        initData();
        initListener();
    }

    protected abstract int setResId();

    protected abstract void initView();

    protected abstract void initData();

    protected abstract void initListener();

    /**
     * 设置activity的toolbar
     * 如果存在就重写
     *
     * @return 页面头部的标题
     */
    protected String setMainTitle() {
        return "";
    }

    /**
     * 初始化toolbar
     *
     * @param title 标题
     */
    private void initToolbar(String title) {
        if (mToolbar == null) {
            return;
        }
        mToolbar.setTitle(title);
        mToolbar.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
        mToolbar.setTitleTextColor(Color.WHITE);
        setSupportActionBar(mToolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }

        ViewPager viewById = findViewById(R.id.viewpager);
        viewById.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackArowEvent();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * 左箭头的点击事件
     */
    protected abstract void onBackArowEvent();

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mUnbinder.unbind();
    }
}
